package com.example.wm.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.entity.BaoXiao;
import com.example.wm.entity.BaoxiaoMx;
import com.example.wm.mapper.ReimbursementMapper;
import com.example.wm.service.ReimbursementDetailsService;
import com.example.wm.service.ReimbursementImagesService;
import com.example.wm.service.ReimbursementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/Reimbursement")
public class ReimbursementController {

    @Resource
    private ReimbursementMapper reimbursementMapper;
    @Resource
    private ReimbursementService reimbursementService;
    @Resource
    private ReimbursementImagesService reimbursementImagesService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private ReimbursementDetailsService reimbursementDetailsService;
    @PostMapping ("/getList")
    public Result<List> getReimbursementList(@RequestBody JSONObject jsonObject){
        String name=jsonObject.getString("name");
        LambdaQueryWrapper<BaoXiao> lq=new LambdaQueryWrapper<>();
        lq.eq(BaoXiao::getStatus,"待审核");
        lq.ne(BaoXiao::getReimbursepp,name);
//        lq.eq(报销::get审核人,jsonObject.getString("shname"));
        lq.orderByDesc(BaoXiao::getSubmittime);
        List<BaoXiao> l1=reimbursementMapper.selectList(lq);
        return  ResultData.success("操作成功",l1);
    }
    @PostMapping ("/update")
    public Result<String> updateReimbursementList(@RequestBody BaoXiao baoXiao){
        reimbursementMapper.updateById(baoXiao);
        return ResultData.success("成功");
    }
    @PostMapping ("/toall")
    public Result<List> toallReimbursementList(@RequestBody JSONObject jsonObject) {

//
//     Set   keys = redisTemplate.keys("reimbursement*");
//        redisTemplate.delete(keys);
        //日期
        String dat = jsonObject.getString("date");
        //年
        String year=jsonObject.getString("year");
        //人员
        String name=jsonObject.getString("pepole");
//        Date d1 = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-");
//        String strDate = sdf.format(d1);
//        if (redisTemplate.hasKey("reimbursement" +strDate+ dat + jsonObject.getString("type"))) {
//            Object a = redisTemplate.opsForValue().get("reimbursement" + strDate+dat + jsonObject.getString("type"));
//            return ResultData.success(a);
//        } else {


        LambdaQueryWrapper<BaoXiao> lq = new LambdaQueryWrapper<>();
        lq.ne(BaoXiao::getStatus, "待审核");
        lq.ne(BaoXiao::getStatus, "已驳回");
        if(!Objects.equals(dat, "全部")){
            lq.between(BaoXiao::getSubmittime, year + "-"+dat+ "-01", year + "-"+ dat + "-31");

        }else{
            lq.between(BaoXiao::getSubmittime, year + "-01-01", year + "-12-31");
        }
        if(!Objects.equals(name, "全部")){
            lq.eq(BaoXiao::getReimbursepp,name);

        }
//        lq.eq(报销::get审核人,jsonObject.getString("shname"));
//        lq.between(BaoXiao::getAudittime, strDate + jsonObject.getString("date") + "-01", strDate + jsonObject.getString("date") + "-31");
//        lq.between(报销::get提交时间, "2023-07-01", "2023-07-31");

        List<BaoXiao> l = reimbursementMapper.selectList(lq);
        System.out.println(l+"按类别查询");
        List<BaoxiaoMx> l2 = null;
        ArrayList<BaoxiaoMx> al = new ArrayList<>();
        Set k = null;
        ArrayList al2 = null;
        for (BaoXiao b : l) {
            LambdaQueryWrapper<BaoxiaoMx> lq2 = new LambdaQueryWrapper<>();
            lq2.eq(BaoxiaoMx::getReimbid, b.getId());
            lq2.eq(BaoxiaoMx::getConsumetype, jsonObject.getString("type"));
//            lq2.eq(报销明细::get消费类型, "其他");
            int n = (int) reimbursementDetailsService.count(lq2);

            if (n > 0) {
                l2 = reimbursementDetailsService.list(lq2);
                al.addAll(l2);
//                System.out.println("count因公司需求垫付报销" + l2.toString());

            }
        }
//            redisTemplate.opsForValue().set("reimbursement" + strDate+dat + jsonObject.getString("type"), al);
        return ResultData.success(al);


//        }

    }

    @PostMapping ("/nametoall")
    public Result<List> nametoallReimbursementList(@RequestBody JSONObject jsonObject) {

        Date d1 = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-");
        String strDate = sdf.format(d1);

        LambdaQueryWrapper<BaoXiao> lq = new LambdaQueryWrapper<>();
        lq.ne(BaoXiao::getStatus, "待审核");
        lq.eq(BaoXiao::getAuditor,jsonObject.getString("shname"));
        lq.between(BaoXiao::getAudittime,strDate+jsonObject.getString("date")+"-01 00:00:00",strDate+jsonObject.getString("date")+"-31 00:00:00");
//        lq.between(报销::get提交时间, "2023-07-01", "2023-07-31");

        List<BaoXiao> l = reimbursementMapper.selectList(lq);

        return ResultData.success(l);
    }
    //保存
    @PostMapping ("/save")
    public Result<Integer> saveReimbursementList(@RequestBody BaoXiao bx) {
        Date d1 = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String strDate = sdf.format(d1);
        Set   keys = redisTemplate.keys("reimbursement"+strDate+"*");
        redisTemplate.delete(keys);


        reimbursementService.save(bx);
        System.out.println(bx.getId());
        return ResultData.success( bx.getId());
    }

    @PostMapping ("/findname")
    public Result<List> findnameReimbursementList(@RequestBody JSONObject jsonObject) {
        LambdaQueryWrapper<BaoXiao> lq = new LambdaQueryWrapper<>();
        lq.eq(BaoXiao::getReimbursepp, jsonObject.getString("name"));
        lq.orderByDesc(BaoXiao::getSubmittime);

       List<BaoXiao> l=reimbursementMapper.selectList(lq);


        return ResultData.success(l);
    }
//根据id删除
@PostMapping ("/delete")
public Result<String> deleteReimbursementList(@RequestBody JSONObject jsonObject) {
    LambdaQueryWrapper<BaoXiao> lq = new LambdaQueryWrapper<>();
        reimbursementService.removeById(jsonObject.getString("id"));

    return ResultData.success("删除成功");
}

}
